package com.spark.incoicings.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spark.incoicings.service.DictDataService;
import com.spark.incoicings.service.SaleCustomerService;
import com.spark.incoicings.service.SalesInvoiceService;
import com.spark.incoicings.service.SalesOrderService;
import com.spark.invoicings.entity.*;
import com.spark.invoicings.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 *
 * 销项发票
 *
 */
@RestController
@RequestMapping("/SalesReceipt")
public class SalesInvoiceController {
    @Resource
    private SalesInvoiceService sis;
    @Resource
    private DictDataService ds;
    @Resource
    private SaleCustomerService sc;
    @Resource
    private SalesOrderService so;

    /**
     * 销项发票条件分页查询
     * */
    @GetMapping("/selectSalesInvoice")
    public Result selectSalesInvoice(
            @RequestParam(defaultValue = "1")Integer pageNum,
            @RequestParam(defaultValue = "6")Integer pageSize,
            @RequestParam(value = "customerId",required = false) Integer customerId,
            @RequestParam(value = "invoiceStatusId",required = false) Integer invoiceStatusId){
        PageHelper.startPage(pageNum,pageSize);
        Map<String,Object> map=new HashMap<>();
        map.put("customerId",customerId);
        map.put("invoiceStatusId",invoiceStatusId);
        PageHelper.orderBy("invoice_id desc");
        List<SalesInvoice> salesInvoices = sis.selectSalesInvoice(map);
        for (SalesInvoice s:salesInvoices){
            s.setDictData(Arrays.asList(ds.getById(s.getInvoiceStatusId()),
                    ds.getById(s.getInvoiceTypeId())));

            s.setSaleCustomer(sc.getById(s.getCustomerId()));

            s.setSalesOrder(so.getById(s.getSalesOrderId()));
        }

        PageInfo<SalesInvoice> page=new PageInfo<>(salesInvoices);
        return Result.success(200,"success",page);
    }

    /**
     * 前端需要的数据开票状态、客户、发票类型
     * */
    @GetMapping("/selectKData")
    public Result selectKData(){
        Map<String, Object> result = new HashMap<>();
        result.put("invoice_status",ds.selectDictType("invoice_status"));
        result.put("customers",sc.list());
        result.put("invoice_type ",ds.selectDictType("invoice_type") );
        return Result.success(200, "success", result);
    }

    @GetMapping("/SalesInvoiceById")
    public Result SalesInvoiceById(Integer id){
        SalesInvoice s = sis.getById(id);
        s.setDictData(Arrays.asList(ds.getById(s.getInvoiceStatusId()),
                ds.getById(s.getInvoiceTypeId())));

        s.setSaleCustomer(sc.getById(s.getCustomerId()));

        s.setSalesOrder(so.getById(s.getSalesOrderId()));
        return Result.success(200, "success", s);
    }

    /**
     * 根据客户id查询销售订单
     * */
    @GetMapping("/selectSOrder")
    public Result selectSOrder(Integer customerId){
        List<SalesOrder> salesOrders = so.selectId(customerId);
        return Result.success(200, "success", salesOrders);
    }

    /**
     * 添加销项发票
     * */
    @PostMapping("/saveSalesInvoice")
    public Result saveSalesInvoice(@RequestBody SalesInvoice salesInvoice){
        SalesOrder s = so.getById(salesInvoice.getSalesOrderId());
        double ykp=salesInvoice.getBillingAmount()+s.getInvoicedAmount();//已开票
        double dkp=s.getOrderTotalAmount()-ykp;//待开票
        s.setInvoicedAmount(ykp);
        s.setPendingInvoicingAmount(dkp);
        so.updateById(s);
        sis.save(salesInvoice);
        return Result.success();
    }

    /**
     * 修改销项发票
     * */
    @PostMapping("/updateSalesInvoice")
    public Result updateSalesInvoice(@RequestBody SalesInvoice salesInvoice){
        sis.updateById(salesInvoice);
        return Result.success();
    }

}
